<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%ns_message}}".
 *
 * @property integer $message_id
 * @property integer $sender_id
 * @property integer $receiver_id
 * @property string $title
 * @property integer $messageText_id
 * @property string $create_date
 * @property integer $isRead
 * @property integer $status
 *
 * @property NsMessageText $messageText
 */
class Message extends \yii\db\ActiveRecord
{
    public $content;
    
    public $sentType;

    public $grade;
    public $department;
    public $professional;
    public $class;

	public $verifyCode;
 
    const STATUS_IN_ALL = 2;//在inbox 和 sent 全部可见
    const STATUS_IN_SENT = 1;//仅在sent中可见
    const STATUS_IN_INBOX = 0;//仅在inbox中可见
    
    const FLAG_READ = 0;
    const FLAG_UNREAD = 1;
    
    const SENTTYPE_ALL = 1;//全部有效用户(status = 1)
    const SENTTYPE_ACTIVE_MONTH = 2;//近一个月内活跃的用户
    const SENTTYPE_ACTIVE_WEEK = 3;//近一个星期内活跃的用户
    const SENTTYPE_ACTIVE_DAY = 4;//近一个天内活跃的用户
    const SENTTYPE_ACTIVE_ONLINE = 5;//当前在线用户

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tbl_message}}';
    }

    public function isRead()
    {
        if($this->isRead == Message::FLAG_READ)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    /**
     * @inheritdoc
     */
    public function isReadText()
    {
        return $this->isRead() == 1 ? yii::t('Message','Read') : yii::t('Message',"Unread");
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['sender_id', 'receiver_id', 'title'], 'required'],
            [['sender_id', 'messageText_id', 'isRead', 'status'], 'integer'],
            [['create_date'], 'safe'],
            [['title'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'message_id' => Yii::t('Message', 'Message ID'),
            'sender_id' => Yii::t('Message', 'Sender ID'),
            'receiver_id' => Yii::t('Message', 'Receiver'),
            'title' => Yii::t('Message', 'Title'),
            'messageText_id' => Yii::t('Message', 'Message Text ID'),
            'create_date' => Yii::t('Message', 'Create Date'),
            'isRead' => Yii::t('Message', 'Is Read'),
            'status' => Yii::t('Message', 'Status'),
            'content' => Yii::t('Message', 'Content'),
            'grade' => Yii::t('Message', 'Grade'),
            'department' => Yii::t('Message', 'Department'),
            'professional' => Yii::t('Message', 'Professional'),
            'class' => Yii::t('Message', 'Class'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMessageText()
    {
        return $this->hasOne(MessageText::className(), ['messageText_id' => 'messageText_id']);
    }
    public function getReceiver()
    {
        return $this->hasOne(User::className(), ['user_id' =>'receiver_id']);
    }
    public function getSender()
    {
        return $this->hasOne(User::className(), ['user_id' => 'sender_id']);
    }

}
